<?php
class ArticleModel
{
    public function __construct()
    {
        $this->db = Yaf_Registry::get('db');
        $this->time = date("Y-m-d H:i:s",time());
    }
    public function selectArticle($start,$everyPage,$article_type_id)
    {
        $sql = "select * from `article`  order by article_id desc  limit {$start},{$everyPage}";
        return $this->db->query($sql);
    }

    public function selectChannel()
    {
        $sql = "select article_type_id,remark,name from collection_robot";
        return $this->db->query($sql);
    }

    public function selectChooceArticle($start,$everyPage,$info )
    {
        if(($info['typical_id'] == 0 && $info['status'] != "" && $info['channel_id'] == 0) ){
            $sql = "select * from `article`  where  status='{$info['status']}'   order by article_id desc  limit {$start},{$everyPage}";
        }
        else if($info['status'] == "" && $info['typical_id'] != 0 && $info['channel_id'] == '0'){
            $sql = "select * from `article`  where article_type_id='{$info['typical_id']}'    order by article_id desc  limit {$start},{$everyPage}";
        }
        else if($info['status'] != "" && $info['typical_id'] != 0 && $info['channel_id'] == '0'){
            $sql = "select * from `article`  where article_type_id='{$info['typical_id']}'  and status='{$info['status']}'  order by article_id desc  limit {$start},{$everyPage}";
        }
        /*else if($info['typical_id'] != 0 && $info['status'] == ""){
            $sql = "select * from `article`  where article_type_id='{$info['typical_id']}'    order by article_id desc  limit {$start},{$everyPage}";
        }*/
        else if($info['typical_id'] != 0 && $info['status'] != "" && $info['channel_id'] == '0'){
            $sql = "select * from `article`  where article_type_id='{$info['typical_id']}' and status='{$info['status']}'    order by article_id desc  limit {$start},{$everyPage}";
        }
        else if($info['typical_id'] == 0 && $info['status'] !="" && $info['channel_id'] == 0){
            
             $sql = "select * from `article`  where  status='{$info['status']}'   order by article_id desc  limit {$start},{$everyPage}";
        }
        else if($info['channel_id'] != "" && $info['typical_id'] == 0 && isset($info['status']) == false){
            $article_info = explode("-", $info['channel_id']);
            $chanel       = trim($article_info[1]," ");
            $sql = "select * from article where chanel='{$chanel}' and article_type_id = '{$article_info[0]}' order by article_id desc  limit {$start},{$everyPage}";
        }
        else if($info['channel_id'] != "" && $info['typical_id'] == 0 && $info['status'] != ""){
            //$article_info = explode("-", $info['channel_id']);
            //$chanel       = trim($article_info[1]," ");
            $sql = "select * from article where chanel='{$info['channel_id']}' and article_type_id = '{$article_info[0]}' and status='{$info['status']}' order by article_id desc  limit {$start},{$everyPage}";
        }
        else if( $info['channel_id'] != '0' && $info['typical_id'] != '0' && isset($info['status']) == false){
            //$article_info = explode("-", $info['channel_id']);
            $sql = "select * from article where chanel='{$info['channel_id']}' and article_type_id = '{$info['typical_id']}' order by article_id desc  limit {$start},{$everyPage}";
        }
        else if( $info['channel_id'] != '0' && $info['typical_id'] != '0' && $info['status'] != ""){
            $article_info = explode("-", $info['channel_id']);
            //$chanel       = trim($article_info[1]," ");
            $sql = "select * from article where chanel='{$info['channel_id']}' and article_type_id = '{$info['typical_id']}' and status='{$info['status']}' order by article_id desc  limit {$start},{$everyPage}";
        }
        else {
            $sql = "select * from `article`     order by article_id desc  limit {$start},{$everyPage}";
        }
       //var_dump($sql);
        return $this->db->query($sql);
    }
    public function screenArticle($type,$status)
    {
        $sql = "select * from article where article_type_id={$type} and status={$status}";
        return $this->db->query($sql);
    }

    public function getArticleInfo()
    {
        $sql = "select title,article_id from article ";
        return $this->db->query($sql);
    }
    public function getAllCount()
    {
        $sql="select count(*) as `rows` from `article`  ";
        return $this->db->query($sql);
    }

    public function getChooceCount($info)
    {
        if(($info['typical_id'] == 0 && $info['status'] != "" && $info['channel_id'] == 0) || empty($info)){
            $sql = "select count(*) as `rows` from `article`  where  status='{$info['status']}'  ";
        }
        else if($info['status'] == "" && $info['typical_id'] != 0 && $info['channel_id'] == '0'){
            $sql = "select count(*) as `rows` from `article` where article_type_id='{$info['typical_id']}'";
        }
        else if($info['status'] != "" && $info['typical_id'] != 0 && $info['channel_id'] == '0'){
            $sql = "select count(*) as `rows` from `article` where article_type_id='{$info['typical_id']}'  and status='{$info['status']}' ";
        }
        else if($info['typical_id'] != 0 && $info['channel_id'] == '0'){
             $sql = "select count(*) as `rows` from `article` where article_type_id='{$info['typical_id']}'";
        }
        else if($info['typical_id'] == 0 && $info['status'] !="" && $info['channel_id'] == '0'){
             $sql = "select count(*) as `rows` from `article` where status='{$info['status']}'";
        }
        else if($info['channel_id'] != "" && isset($info['status']) == false){
            $sql = "select count(*) as `rows` from `article` where chanel='{$info['channel_id']}' and article_type_id = '{$info['typical_id']}' ";
        }

        else if($info['channel_id'] != ""  && $info['typical_id'] == 0 && $info['status'] != ""){
            $sql = "select count(*) as `rows` from `article` where chanel='{$info['channel_id']}' and article_type_id = '{$info['typical_id']}'   and status='{$info['status']}' ";
        }

        else if($info['channel_id'] != '0'  && $info['typical_id'] != 0 && $info['status'] != ""){
            $sql = "select count(*) as `rows` from `article` where chanel='{$info['channel_id']}' and article_type_id = '{$info['typical_id']}'   and status='{$info['status']}' ";
        }

        else{
            $sql = "select count(*) as `rows` from `article`";
        }
        //var_dump($sql);
        return $this->db->query($sql);
    }

    public function articleType()
    {
        $sql="select * from article_type";
        return $this->db->query($sql);
    }

    public function del($id)
    {
        $sql = "delete from `article` where article_id='{$id}' ";
        return $this->db->query($sql);
    }

    public function edit($info, $id)
    {
        //var_dump($info);
        $sql = "update `article` set `title`='{$info['title']}', `article_type_id`='{$info['article_type_id']}',`author`='{$info['author']}', `source`='{$info['source']}',`status`='{$info['status']}',`content`='{$info['content']}',`create_time`=UNIX_TIMESTAMP('{$this->time}') WHERE article_id='{$id}'";
        return $this->db->query($sql);
    }

    public function push($info)
    {
        
        foreach ($info['articleId']  as $key => $value) {
             $sql = "update `article` set `push_time`=UNIX_TIMESTAMP('{$this->time}')   where article_id='{$value}' ";
             $pushTime = $this->db->query($sql);  
        }
        return $pushTime;
    }
    

    public function getArticleById($id)
    {

        //$sql="select *,`article_type`.`name` from `collection_content`,`article_type` where `collection_content`.`article_type_id` = `article_type`.`article_type_id` and `content_id`='{$id}'";
        $sql="select * from `article` where  `article_id`='{$id}'";
        return $this->db->query($sql);
    }

    public function selectChannelByRobot($info)
    {
        $sql="select name,remark from collection_robot where article_type_id = '{$info['article_type_id']}' ";
        return $this->db->query($sql);
    }

    public function getTypicalName()
    {
        $sql="select * from `article_type` ";
        return $this->db->query($sql);
    }

    public function selectSites(){
        $sql="select * from `sites`";
        return $this->db->query($sql);
    }

     public function status($info){
        //$article = $info['articleId'];
        foreach ($info as $key => $value) {
            foreach ($value as $k => $v) {
                if($info['status'] == "Yes"){
                    $sql="update `article` set `status`='2' where `article_id`='{$v}'";
                }
                else if($info['status'] == "No"){
                    $sql="update `article` set `status`='1' where `article_id`='{$v}'";
                }
                 
            }
            $result = $this->db->query($sql);
        }
        return $result;
    }

    public function updatePush($info){
        foreach ($info as $key => $value) {
            foreach ($value as $k => $v) {
                if($info['status'] == "Yes"){
                    $sql="update `push_log` set `status`='2' where `article_id`='{$v}'";
                }
                else if($info['status'] == "No"){
                    $sql="update `push_log` set `status`='1' where `article_id`='{$v}'";
                }
                 
            }
            $result = $this->db->query($sql);
        }
        return $result;
    }

    public function addPush($info)
    {
        /*foreach ($info as $k=>$v) 
        {*/
            //var_dump($v);
            /*foreach ($info['article_id'] as $key=>$value) 
            {*/
                
                foreach ($info['article_title'] as $ks => $vs) {
                    //var_dump($vs);
                    $sql="insert into push_log(`user_id`,`article_id`,`create_time`,`status`,`siteid`,`catid`,`article_title`) values('{$info['user_id']}','{$info['article_id'][$ks]}',UNIX_TIMESTAMP('{$info['time']}'),'{$info['status']}','{$info['sites']}','{$info['oneModel']}','{$vs}')";
                    $result = $this->db->query($sql);
                }
                
            //}
        //}
       return $result;
    }
}
?>